<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class Printer extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_printer';



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

//        if(empty($data)){
//
//            $this->dataAdd($dis);
//
//            $data = $this->where($dis)->find();
//
//        }

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-11 09:25
     * @功能说明:订单打印
     */
    public function userOrder($id,$aotu=1,$add_time=0){

        $order_model   = new Order();

        $address_model = new OrderAddress();

        $order_goods_model = new OrderGoods();
        //订单信息
        $order   = $order_model->orderInfo(['id'=>$id]);

        if($add_time!=0){

            $order['order_goods'] = $order_goods_model->where(['order_id'=>$order['id'],'add_foods_time'=>$add_time])->select()->toArray();
        }
        //地址信息
        $address = $address_model->dataInfo(['order_id'=>$order['id']]);

        switch ($order['order_type']){

            case 0:
                $order_type = '外卖订单';
                break;
            case 1:
                $order_type = '堂食订单';
                break;
            case 2:
                $order_type = '自提订单';
                break;
        }

        $brs = "<BR>";

        $orderInfo = '<CB>'.$order_type.'</CB><BR>';

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '<B>'. $address['name'].'('.substr_replace($address['phone'],'****',3,4).')</B>'.$brs;

        if($order['order_type']==1){

            $table_model = new Table();

            $table_name = $table_model->where(['id'=>$order['table_id']])->value('title');

            $orderInfo .= '<B>'.'桌号:'.$table_name.'</B>'.$brs;

            $orderInfo .= '<B>'.'人数:'.$order['user_num'].'</B>'.$brs;

            if($add_time!=0){

                $orderInfo .= '<B>加菜</B>'.$brs;

            }

        }else{

            $orderInfo .= '<B>'.'地址:'.$address['area'].$address['info'].'</B>'.$brs;

        }

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '商品/规格（加料）/数量/价格'.$brs;

        $orderInfo .= '--------------------------------'.$brs;

        $num = 0;

        foreach ($order['order_goods'] as $v) {

            $num += $v['goods_num'];

            $v['goods_name'] = mb_convert_encoding($v['goods_name'], "UTF-8", "auto");

            if(!empty($v['feeding_text'])){

                $orderInfo.= $v['goods_name'].'     '.$v['norms'].'('.$v['feeding_text'].')    x'.$v['goods_num'].'    '.round($v['goods_price']*$v['goods_num'],2).'元'.$brs;

            }else{

                $orderInfo.= $v['goods_name'].'     '.$v['norms'].'    x'.$v['goods_num'].'    '.round($v['goods_price']*$v['goods_num'],2).'元'.$brs;

            }

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;

        }

        $orderInfo .= '--------------------------------'.$brs;

        if(!empty($order['pay_time'])){

            $orderInfo .= '付款时间:'.date('Y-m-d H:i:s',$order['pay_time']).$brs;
        }

        $orderInfo .= ''.$brs;

        $orderInfo .= '共'.$num.'件'.$brs;

        $orderInfo .= ''.$brs;

        $orderInfo .= '订单号:' . substr_replace($order['order_code'],'****',3,15) .$brs;

        if(!empty($order['coupon_price'])){

            $orderInfo .= ''.$brs;

            $orderInfo .= '优惠券抵扣:' . $order['coupon_price'] . '元'.$brs;
        }

        if($order['order_type']==0){

            $orderInfo .= ''.$brs;

            $orderInfo .= '配送费:' . $order['express_price'] . '元'.$brs;
        }

        if($order['order_type']==1){

            $orderInfo .= '餐位费:' . $order['meal_fee'] . '元'.$brs;
        }

        $orderInfo .= ''.$brs;

        $orderInfo .= '支付金额:' . $order['pay_price'] . '元'.$brs;

        $orderInfo .= ''.$brs;

        if(!empty($order['order_text'])){

            $orderInfo .= '备注:' . $order['order_text'].$brs;
        }

        return $orderInfo;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-11 17:40
     * @功能说明:后厨小票
     */
    public function kitchenOrder($id,$aotu=1,$add_time=0){

        $order_model   = new Order();
        //订单信息
        $order   = $order_model->orderInfo(['id'=>$id]);

        $order_goods_model = new OrderGoods();

        if($add_time!=0){

            $order['order_goods'] = $order_goods_model->where(['order_id'=>$order['id'],'add_foods_time'=>$add_time])->select()->toArray();
        }

        switch ($order['order_type']){

            case 0:
                $order_type = '外卖订单(后厨小票)';
                break;
            case 1:
                $order_type = '堂食订单(后厨小票)';
                break;
            case 2:
                $order_type = '自提订单(后厨小票)';
                break;
        }

        $brs = "<BR>";

        $orderInfo = '<CB>'.$order_type.'</CB><BR>';

        $orderInfo .= ''.$brs;

        $orderInfo .= '订单号:' . substr_replace($order['order_code'],'****',3,15) .$brs;

        if($order['order_type']==1){

            $table_model = new Table();

            $table_name = $table_model->where(['id'=>$order['table_id']])->value('title');

            $orderInfo .= '<B>'.'桌号:'.$table_name.'</B>'.$brs;

            $orderInfo .= '<B>'.'人数:'.$order['user_num'].'</B>'.$brs;

            if($add_time!=0){

                $orderInfo .= '<B>加菜</B>'.$brs;

            }

        }

        if(!empty($order['pay_time'])){

            $orderInfo .= '付款时间:'.date('Y-m-d H:i:s',$order['pay_time']).$brs;
        }

        $orderInfo .= ''.$brs;

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '商品/规格（加料）/数量/价格'.$brs;

        $orderInfo .= '--------------------------------'.$brs;

        $num = 0;

        foreach ($order['order_goods'] as $v) {

            $num += $v['goods_num'];

            $v['goods_name'] = mb_convert_encoding($v['goods_name'], "UTF-8", "auto");

            if(!empty($v['feeding_text'])){

                $orderInfo.= $v['goods_name'].'     '.$v['norms'].'('.$v['feeding_text'].')    x'.$v['goods_num'].'    '.round($v['goods_price']*$v['goods_num'],2).'元'.$brs;

            }else{

                $orderInfo.= $v['goods_name'].'     '.$v['norms'].'    x'.$v['goods_num'].'    '.round($v['goods_price']*$v['goods_num'],2).'元'.$brs;

            }

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;

        }

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '共'.$num.'件'.$brs;

        $orderInfo .= ''.$brs;

        if(!empty($order['order_text'])){

            $orderInfo .= '<B>'.'备注:' . $order['order_text'].'</B>'.$brs;
        }

        return $orderInfo;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-11 10:20
     * @功能说明:
     */
    public function storeOrderPrinter($store_id,$orderInfo,$kitchen,$aotu=1){

        $dis = [

            'store_id' => $store_id,

            'status'   => 1,
        ];

        if($aotu==1){

            $dis['auto'] =1;
        }

        $printer_config = $this->where($dis)->select()->toArray();

        if(!empty($printer_config)){

            foreach ($printer_config as $value){

                $value['ukey'] = $value['api_key'];

                $value['sn']   = $value['printer_key'];

                if($value['user_ticket']==1){
                    //用户小票
                    $res = \longbingcore\printer\Printer::FeiePrintMsg($value,$orderInfo,'Open_printMsg',$value['user_ticket_num']);

                }

                if($value['kitchen_ticket']==1){
                    //后台小票
                    $res = \longbingcore\printer\Printer::FeiePrintMsg($value,$kitchen,'Open_printMsg',$value['kitchen_ticket_num']);

                }

            }

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 09:57
     * @功能说明:打印
     */
    public function printer($id,$aotu=1,$add_time=0){

        //用户小票
        $user_order    = $this->userOrder($id,$aotu,$add_time);
        //后厨小票
        $kitchen_order = $this->kitchenOrder($id,$aotu,$add_time);

        $order_model = new Order();

        $store_id = $order_model->where(['id'=>$id])->value('store_id');
        //打印
        $res = $this->storeOrderPrinter($store_id,$user_order,$kitchen_order,$aotu);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 10:22
     * @功能说明:订单取消打印机通知
     */
    public function cancelOrderPrinter(){



    }








}